<template>
  <div class="pdf-viewer-example">
    <div class="control-panel">
      <!-- NPM包信息移到顶部 -->
      <div class="npm-info">
        <div class="npm-header">
          <h3>📦 Vue3 PDF.js Viewer</h3>
          <div class="npm-details">
            <span class="npm-badge">v1.0.3</span>
            <span class="author-badge">Ning,Li</span>
          </div>
        </div>
        <div class="status-info">
          <span class="status-success">✅ NPM包已加载</span>
          <span class="status-success">✅ 组件正常运行</span>
        </div>
      </div>
      
      <div class="control-sections">
        <div class="control-group">
          <label>📄 PDF文件路径:</label>
          <input 
            v-model="pdfSrc" 
            type="text" 
            placeholder="输入PDF文件路径"
            class="input-field"
          >
          <div class="button-group">
            <button @click="loadEmptyPdf" class="test-btn">空路径测试</button>
            <button @click="loadInvalidPdf" class="test-btn">无效路径测试</button>
          </div>
        </div>

        <div class="control-group">
          <label>🎮 按钮控制:</label>
          <div class="checkbox-group">
            <label class="checkbox-label">
              <input v-model="showDownload" type="checkbox">
              <span>显示下载按钮</span>
            </label>
            <label class="checkbox-label">
              <input v-model="showPrint" type="checkbox">
              <span>显示打印按钮</span>
            </label>
            <label class="checkbox-label">
              <input v-model="showFullscreen" type="checkbox">
              <span>显示全屏按钮</span>
            </label>
            <label class="checkbox-label">
              <input v-model="showRetry" type="checkbox">
              <span>显示重试按钮</span>
            </label>
          </div>
        </div>

        <div class="control-group">
          <label>🎨 主题配置:</label>
          <div class="color-group">
            <div class="color-item">
              <label>工具栏背景</label>
              <input v-model="toolbarBg" type="color">
            </div>
            <div class="color-item">
              <label>工具栏文字</label>
              <input v-model="toolbarText" type="color">
            </div>
            <div class="color-item">
              <label>内容背景</label>
              <input v-model="contentBg" type="color">
            </div>
            <div class="color-item">
              <label>主题色</label>
              <input v-model="primaryColor" type="color">
            </div>
          </div>
        </div>

        <div class="control-group">
          <label>✨ 预设主题:</label>
          <div class="theme-buttons">
            <button @click="applyDarkTheme" class="theme-btn dark">深色主题</button>
            <button @click="applyLightTheme" class="theme-btn light">浅色主题</button>
            <button @click="applyBlueTheme" class="theme-btn blue">蓝色主题</button>
            <button @click="applyGreenTheme" class="theme-btn green">绿色主题</button>
          </div>
        </div>

        <div class="control-group">
          <label>🚀 快速选择PDF:</label>
          <div class="pdf-quick-select">
            <button @click="pdfSrc = '/test1.pdf'" class="pdf-btn" :class="{ active: pdfSrc === '/test1.pdf' }">
              📄 测试文档2
            </button>
            <button @click="pdfSrc = '/test2.pdf'" class="pdf-btn" :class="{ active: pdfSrc === '/test2.pdf' }">
              📄 测试文档3
            </button>
          </div>
        </div>
      </div>
    </div>

    <div class="pdf-container">
      <div class="pdf-header">
        <h4>📖 PDF预览器</h4>
        <div class="pdf-info">
          <span v-if="pdfSrc" class="current-pdf">当前文件: {{ getPdfName(pdfSrc) }}</span>
          <span v-else class="no-pdf">请选择PDF文件</span>
        </div>
      </div>
      <div class="pdf-viewer-wrapper">
        <PdfWithThumbnails
          :src="pdfSrc"
          :show-download-btn="showDownload"
          :show-print-btn="showPrint"
          :show-fullscreen-btn="showFullscreen"
          :show-retry-btn="showRetry"
          :toolbar-background-color="toolbarBg"
          :toolbar-text-color="toolbarText"
          :content-background-color="contentBg"
          :thumbnail-background-color="thumbnailBg"
          :thumbnail-item-background-color="thumbnailItemBg"
          :primary-color="primaryColor"
          :button-hover-color="buttonHover"
          @pdf-loaded="onPdfLoaded"
          @pdf-error="onPdfError"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { PdfWithThumbnails } from 'vue3-pdfjs-viewer';

/**
 * PDF查看器使用示例组件
 * @author Ning,Li
 */

// PDF源文件路径
const pdfSrc = ref('/test1.pdf');

// 按钮显示控制
const showDownload = ref(true);
const showPrint = ref(true);
const showFullscreen = ref(true);
const showRetry = ref(true);

// 主题颜色配置
const toolbarBg = ref('#333333');
const toolbarText = ref('#ffffff');
const contentBg = ref('#222222');
const primaryColor = ref('#42b983');

// 计算衍生颜色
const thumbnailBg = computed(() => {
  const color = contentBg.value;
  return adjustBrightness(color, 0.1);
});

const thumbnailItemBg = computed(() => {
  return '#f5f5f5';
});

const buttonHover = computed(() => {
  const color = toolbarBg.value;
  return adjustBrightness(color, 0.2);
});

/**
 * 获取PDF文件名
 * @author Ning,Li
 */
const getPdfName = (path) => {
  if (!path) return '';
  return path.split('/').pop() || path;
};

/**
 * 调整颜色亮度
 * @author Ning,Li
 */
function adjustBrightness(hex, factor) {
  hex = hex.replace('#', '');
  const r = parseInt(hex.substr(0, 2), 16);
  const g = parseInt(hex.substr(2, 2), 16);
  const b = parseInt(hex.substr(4, 2), 16);
  const newR = Math.max(0, Math.min(255, Math.round(r + (255 - r) * factor)));
  const newG = Math.max(0, Math.min(255, Math.round(g + (255 - g) * factor)));
  const newB = Math.max(0, Math.min(255, Math.round(b + (255 - b) * factor)));
  return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
}

/**
 * 测试空PDF路径
 * @author Ning,Li
 */
const loadEmptyPdf = () => {
  pdfSrc.value = '';
};

/**
 * 测试无效PDF路径
 * @author Ning,Li
 */
const loadInvalidPdf = () => {
  pdfSrc.value = '/invalid-file.pdf';
};

/**
 * 应用深色主题
 * @author Ning,Li
 */
const applyDarkTheme = () => {
  toolbarBg.value = '#1a1a1a';
  toolbarText.value = '#ffffff';
  contentBg.value = '#0f0f0f';
  primaryColor.value = '#4fc3f7';
};

/**
 * 应用浅色主题
 * @author Ning,Li
 */
const applyLightTheme = () => {
  toolbarBg.value = '#f5f5f5';
  toolbarText.value = '#333333';
  contentBg.value = '#ffffff';
  primaryColor.value = '#2196f3';
};

/**
 * 应用蓝色主题
 * @author Ning,Li
 */
const applyBlueTheme = () => {
  toolbarBg.value = '#1976d2';
  toolbarText.value = '#ffffff';
  contentBg.value = '#e3f2fd';
  primaryColor.value = '#ff5722';
};

/**
 * 应用绿色主题
 * @author Ning,Li
 */
const applyGreenTheme = () => {
  toolbarBg.value = '#2e7d32';
  toolbarText.value = '#ffffff';
  contentBg.value = '#e8f5e8';
  primaryColor.value = '#42b983';
};

/**
 * PDF加载成功事件处理
 * @author Ning,Li
 */
const onPdfLoaded = () => {
  console.log('PDF加载成功');
};

/**
 * PDF加载失败事件处理
 * @author Ning,Li
 */
const onPdfError = (error) => {
  console.error('PDF加载失败:', error);
};
</script>

<style scoped>
.pdf-viewer-example {
  display: flex;
  height: calc(100vh - 220px);
  background: #f8f9fa;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  margin: 20px 20px 20px 20px;
  /* 确保组件占满可用空间 */
  flex: 1;
}

.control-panel {
  width: 400px;
  min-width: 350px;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #e9ecef;
  box-shadow: 2px 0 20px rgba(0,0,0,0.08);
  /* 优化滚动性能 */
  overflow-y: auto;
  overflow-x: hidden;
  max-height: 100%;
  /* 添加flex-shrink防止压缩过小 */
  flex-shrink: 0;
}

/* 控制面板滚动条样式 */
.control-panel::-webkit-scrollbar {
  width: 8px;
}

.control-panel::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.control-panel::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, #42b983, #369870);
  border-radius: 4px;
}

.control-panel::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(135deg, #369870, #2d7a5f);
}

/* Firefox滚动条样式 */
.control-panel {
  scrollbar-width: thin;
  scrollbar-color: rgba(66, 185, 131, 0.6) #f1f1f1;
}

.npm-info {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
  flex-shrink: 0;
}

.npm-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.npm-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.npm-details {
  display: flex;
  gap: 8px;
}

.npm-badge, .author-badge {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  background: rgba(255,255,255,0.2);
  backdrop-filter: blur(10px);
}

.status-info {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.status-success {
  padding: 4px 10px;
  background: rgba(46, 204, 113, 0.2);
  color: #ffffff;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid rgba(255,255,255,0.3);
}

.control-sections {
  flex: 1;
  padding: 0 25px;
  overflow-y: auto;
  overflow-x: hidden;
  /* 确保滚动条样式美观 */
  scrollbar-width: thin;
  scrollbar-color: rgba(66, 185, 131, 0.6) #f1f1f1;
}

.control-group {
  margin-bottom: 25px;
  padding-top: 20px;
}

.control-group:first-of-type {
  padding-top: 25px;
}

.control-group label {
  display: block;
  font-weight: 600;
  color: #495057;
  margin-bottom: 12px;
  font-size: 14px;
}

.input-field {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 12px;
  transition: all 0.3s ease;
  background: #fff;
}

.input-field:focus {
  outline: none;
  border-color: #42b983;
  box-shadow: 0 0 0 3px rgba(66, 185, 131, 0.1);
}

.button-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.test-btn {
  background: linear-gradient(135deg, #ff9800, #f57c00);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(255, 152, 0, 0.3);
}

.test-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(255, 152, 0, 0.4);
}

.checkbox-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-weight: 500;
  margin-bottom: 0;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  background: #f8f9fa;
}

.checkbox-label:hover {
  background-color: #e9ecef;
  border-color: #42b983;
  transform: translateX(4px);
}

.checkbox-label input {
  margin-right: 12px;
  width: 18px;
  height: 18px;
  accent-color: #42b983;
}

.color-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.color-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.color-item label {
  font-size: 12px;
  margin-bottom: 0;
  color: #6c757d;
  font-weight: 500;
}

.color-item input[type="color"] {
  width: 100%;
  height: 44px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.color-item input[type="color"]:hover {
  border-color: #42b983;
  transform: scale(1.05);
}

.theme-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.theme-btn {
  color: white;
  border: none;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.theme-btn.dark {
  background: linear-gradient(135deg, #2c3e50, #34495e);
}

.theme-btn.light {
  background: linear-gradient(135deg, #ecf0f1, #bdc3c7);
  color: #2c3e50;
}

.theme-btn.blue {
  background: linear-gradient(135deg, #3498db, #2980b9);
}

.theme-btn.green {
  background: linear-gradient(135deg, #27ae60, #2ecc71);
}

.theme-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

.pdf-quick-select {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pdf-btn {
  background: linear-gradient(135deg, #42b983, #369870);
  color: white;
  border: none;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
  text-align: left;
  box-shadow: 0 2px 8px rgba(66, 185, 131, 0.3);
}

.pdf-btn:hover {
  transform: translateY(-2px) translateX(4px);
  box-shadow: 0 4px 16px rgba(66, 185, 131, 0.4);
}

.pdf-btn.active {
  background: linear-gradient(135deg, #27ae60, #2ecc71);
  box-shadow: 0 4px 16px rgba(39, 174, 96, 0.4);
}

.pdf-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  position: relative;
  min-width: 0; /* 允许flex项目收缩 */
  overflow: hidden;
}

.pdf-header {
  background: linear-gradient(135deg, #f8f9fa, #e9ecef);
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.pdf-header h4 {
  margin: 0;
  color: #495057;
  font-weight: 600;
  font-size: 16px;
}

.pdf-info {
  font-size: 14px;
  color: #6c757d;
}

.current-pdf {
  color: #28a745;
  font-weight: 500;
}

.no-pdf {
  color: #dc3545;
  font-style: italic;
}

.pdf-viewer-wrapper {
  flex: 1;
  background: #222;
  position: relative;
  overflow: hidden; /* 去掉外层滚动条，由PDF内容直接处理滚动 */
  min-height: 0; /* 允许flex项目收缩 */
}

/* 确保PDF组件内部工具栏固定 */
.pdf-viewer-wrapper :deep(.pdfjs-toolbar) {
  position: sticky !important;
  top: 0 !important;
  z-index: 100 !important;
  background: #333 !important;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2) !important;
}

/* 确保PDF内容区域结构正确 */
.pdf-viewer-wrapper :deep(.pdfjs-main) {
  display: flex !important;
  flex: 1 !important;
  min-height: 0 !important;
  overflow: hidden !important;
}

/* 确保PDF内容区域可以独立滚动 */
.pdf-viewer-wrapper :deep(.pdfjs-content) {
  flex: 1 !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  height: 100% !important;
  min-height: 0 !important;
}

/* 确保缩略图区域独立滚动 */
.pdf-viewer-wrapper :deep(.pdfjs-thumbnails) {
  overflow-y: auto !important;
  overflow-x: hidden !important;
  height: 100% !important;
}

/* 通用工具栏样式覆盖 */
.pdf-viewer-wrapper :deep(.toolbar) {
  position: sticky !important;
  top: 0 !important;
  z-index: 100 !important;
}

/* 平板设备优化 */
@media (max-width: 1200px) {
  .pdf-viewer-example {
    margin: 10px;
    max-width: calc(100vw - 20px);
  }
  
  .control-panel {
    width: 350px;
    min-width: 320px;
  }
}

/* 中等屏幕设备优化 */
@media (max-width: 1024px) {
  .pdf-viewer-example {
    flex-direction: column;
    height: auto;
    min-height: 90vh;
    margin: 5px;
    border-radius: 12px;
    max-width: calc(100vw - 10px);
    max-height: none;
  }
  
  .control-panel {
    width: 100%;
    min-width: auto;
    max-height: 40vh;
    border-right: none;
    border-bottom: 1px solid #e9ecef;
    flex-shrink: 0;
  }
  
  .pdf-container {
    height: 60vh;
    min-height: 500px;
  }
  
  .control-sections {
    padding: 0 20px;
  }
}

/* 移动设备优化 */
@media (max-width: 768px) {
  .pdf-viewer-example {
    margin: 0;
    border-radius: 0;
    min-height: 100vh;
    max-width: 100vw;
  }
  
  .control-panel {
    max-height: 35vh;
  }
  
  .pdf-container {
    height: 65vh;
    min-height: 400px;
  }
  
  .control-sections {
    padding: 0 15px;
  }
  
  .color-group {
    grid-template-columns: 1fr;
  }
  
  .theme-buttons {
    grid-template-columns: 1fr;
  }
  
  .npm-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .npm-details {
    align-self: stretch;
    justify-content: space-between;
  }
  
  .control-group {
    margin-bottom: 20px;
    padding-top: 15px;
  }
}

/* 小屏幕设备优化 */
@media (max-width: 480px) {
  .control-panel {
    max-height: 30vh;
  }
  
  .pdf-container {
    height: 70vh;
    min-height: 350px;
  }
  
  .control-sections {
    padding: 0 12px;
  }
  
  .npm-info {
    padding: 15px;
  }
  
  .pdf-header {
    padding: 15px;
  }
  
  .pdf-header h4 {
    font-size: 14px;
  }
  
  .pdf-info {
    font-size: 12px;
  }
  
  .control-group {
    margin-bottom: 18px;
    padding-top: 12px;
  }
  
  .control-group label {
    font-size: 13px;
    margin-bottom: 10px;
  }
  
  .input-field {
    padding: 10px 14px;
    font-size: 13px;
  }
  
  .checkbox-label {
    padding: 10px;
    font-size: 13px;
  }
}

/* 超小屏幕设备优化 */
@media (max-width: 320px) {
  .control-sections {
    padding: 0 10px;
  }
  
  .npm-info {
    padding: 12px;
  }
  
  .pdf-header {
    padding: 12px;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .control-group {
    margin-bottom: 15px;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .test-btn {
    width: 100%;
    text-align: center;
  }
}

/* 横屏优化 */
@media (max-height: 600px) and (orientation: landscape) {
  .pdf-viewer-example {
    min-height: 95vh;
  }
  
  .control-panel {
    max-height: 45vh;
  }
  
  .pdf-container {
    height: 55vh;
    min-height: 300px;
  }
}

/* 确保在极小屏幕上也能正常显示 */
@media (max-width: 280px) {
  .npm-header h3 {
    font-size: 16px;
  }
  
  .npm-badge, .author-badge {
    font-size: 10px;
    padding: 3px 8px;
  }
  
  .status-success {
    font-size: 10px;
    padding: 3px 8px;
  }
}</style> 